<!-- 在这个文件对每个tab对应的列表进行渲染 -->
<template>
	<view class="paryorder-content">
		<!-- 这里设置了z-paging加载时禁止自动调用reload方法，自行控制何时reload（懒加载）-->
		<!--  :enable-back-to-top="currentIndex===tabIndex" 在微信小程序上可以多加这一句，因为默认是允许点击返回顶部的，但是这个页面有多个scroll-view，会全部返回顶部，所以需要控制是当前index才允许点击返回顶部 -->
		<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" :auto="true">
			<template #top>
				<view class=""  style="background-color: #F8F8F8;">
					<u-navbar :border-bottom="false" title="品牌" :background="{'background':'#F8F8F8'}" :title-size="36" :title-bold="true" :title-color="textColor">
						
						<template #right>
							<view class="u-p-r-20" @click="editTemplateTapFn('add')" v-if="!isChoose">
								新增
							</view>
						</template>
					</u-navbar>
					<view class="u-p-20" style="background-color: #F8F8F8;">
						<u-search :searchIconFlag="true" v-model="searchText" input-align="center" placeholder="品牌名称" height="70" :border-color="'#FFBB1A'" :bg-color="'#F8F8F8'" 	:show-action="false" @change="reloadFn" @search="reloadFn"></u-search>
					</view>
				</view>
			</template>
			
			<!-- 如果希望其他view跟着页面滚动，可以放在z-paging标签内 -->
			<view class="u-p-35 u-flex u-row-between" style="width: 100%;flex-wrap: wrap;">
				<view class="payorder-container" @click="chooseBrand(item.name,item.id)" :style="{'background':isChoose&&item.id===backSendData.id?'#ffbb1a':'#fff'}" v-for="(item,index) in dataList" :key="index" >
					<view class="payorder-section">
						<view class="u-flex" style="justify-content: flex-end;width:100%;">
							<image src="/static/xiaoshou/delPrice.png" v-if="!isChoose"  style="width: 36rpx; height: 36rpx;" @click="delTemplateTap(item.id)"></image>
						</view>
						<image :src="item.url?item.url:defaultLogo" mode=""  class="section-image" ></image>
						<view class="section-list ">
							<view class="section-item ">
								<view class="section-item-name u-text-center u-line-1" style="width:100%;">
								{{item.name}}
								<u-icon  v-if="!isChoose" name="edit-pen" class="u-m-l-14" @click="editTemplateTapFn('edit',item.name,item.id,item.url,item.logoResourceId)"></u-icon>
								</view>
								<view class="u-flex u-row-center">
									<view class="u-font-24 u-m-t-10 " style="margin: 0 auto; display: inline-block; color: #999;background-color: #f8f8f8;padding: 10rpx 12rpx;border-radius: 10rpx;">
										关联{{item.itemCount}}个商品
									</view>
								</view>
								
							</view>
						</view>
						<!-- <view class="u-flex u-col-center u-row-between u-m-l-20" style="height: 100%;">
							<view class="section-btn-box " @click="$mRouter.push({ route: '/pages/owners/priceMange/priceDetails?item='+JSON.stringify(item)})">
								<image src="/static/owners/price.png" mode="" style="height: 46rpx;width: 46rpx;"></image>
							</view>
						</view> -->
					</view>
				</view>
			</view>
			<view slot="bottom" v-if="isChoose" class="">
				<view class="btn-bottom-group">
					<u-button type="default" :custom-style="customStyle" :hover-class="'none'" @click="backSendFn">确定</u-button>
					<u-button  :custom-style="cancelStyle" :plain="false" :hover-class="'none'" @click="cancelSendFn" class="u-m-t-35" >取消</u-button>
				</view>
				
			</view>
		</z-paging>
		<u-modal v-model="delModel" :content="'您确定要移除此品牌吗？'"  @confirm="delTemplateFn()" ></u-modal>
		<u-modal v-model="editTemplate" z-index="11" ref="editModel" :async-close="true"	  @confirm="editTemplateFn()" :title="editType==='add'?'新增品牌':'修改品牌名称'">
			<view class="u-p-35 u-flex" style="flex-wrap: wrap;justify-content: center;">
				<UploadImage :fileList="imageList" @onSuccess="uploadSuccess" @onRemove="uploadRemove"></UploadImage>
				<view class="u-flex u-m-t-20" style="width:100%">
					<text>名称：</text>
					<u-input  v-model="templateName" placeholder="请输入品牌名称" border type="text" />
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import { useShopStore } from '@/store/shop.js';
	import { mapState } from 'pinia';
	import UploadImage from '@/components/upload-image/index.vue';
	export default {
		components:{UploadImage},
		data() {
			return {
				//v-model绑定的这个变量不要在分页请求结束中自己赋值！！！
				dataList: [],
				searchText:'',
				editTemplate:false,
				delModel:false,
				templateName:'',
				chooseTemplateId:'',
				editType:'',
				imageList:[],
				logoResourceId:'',
				isChoose:false,
				backSendData:{
					name:'',
					id:'',
				},
				customStyle:{
					color:'#fff',
					background:'#ffbb1a',
					borderRadius:'100rpx',
					height:'88rpx',
					lineHeight:'88rpx',
					fontSize:'34rpx'
				},
				cancelStyle:{
					height:'88rpx',
					borderRadius:'100rpx',
					lineHeight:'88rpx',
					fontSize:'34rpx'
				},
			}
		},
		onLoad(options) {
			if(options.isChoose == 'true'){
				this.isChoose = true;
				// this.$store.commit('shop/SET_CHOOSEITEM_ID',options.id);
				this.backSendData.name = options.name;
				this.backSendData.id = options.id;
			}else{
				this.isChoose = false;
			}
		},
		computed:{
			...mapState(useShopStore, {storeShop: 'shop'})
		},
		methods: {
			chooseBrand(name,id){
				if(this.isChoose){
					this.backSendData.name = name;
					this.backSendData.id = id;
				}
			},
			backSendFn(){
				uni.$emit('sendBrandData', this.backSendData)
				uni.navigateBack({
					delta: 1
				})
			},
			cancelSendFn(){
				this.backSendData.name = '';
				this.backSendData.id = '';
				uni.$emit('sendBrandData', this.backSendData)
				uni.navigateBack({
					delta: 1
				});
			},
			reloadFn(){
				this.$refs.paging.reload();
			},
			uploadSuccess(val){
				this.logoResourceId = val;
			},
			uploadRemove(val){
				this.logoResourceId = '';
			},
			// 编辑品牌名称弹窗开启
			editTemplateTapFn(type,name,id,url,logoId){
				if(type === 'add'){
					this.chooseTemplateId = '';
					this.templateName = '';
					this.imageList= [];
				}else{
					this.chooseTemplateId = id;
					this.templateName = name;
					let ary = [];
					ary.push({
						url:url
					})
					this.imageList= ary;
					this.logoResourceId = logoId;
				}
				this.editType = type;
				this.editTemplate = true;
			},
			async editTemplateFn(){
				console.log(this.logoResourceId);
				// uni.showLoading({
				// 	title:"loading..."
				// });
				// let params = {
				// 	name:this.templateName
				// }
				// if(this.logoResourceId !== ''){
				// 	params.logoResourceId = this.logoResourceId;
				// }else{
				// 	uni.hideLoading();
				// 	this.$u.toast("品牌logo不得为空!");
				// 	this.$refs.editModel.clearLoading();
				// 	return false;
				// }
				// if(this.templateName === ''){
				// 	uni.hideLoading();
				// 	this.$u.toast("品牌名称不得为空!");
				// 	this.$refs.editModel.clearLoading();
				// 	return false;
				// }
				// if(this.editType === 'edit'){
				// 	params.id = this.chooseTemplateId;
				// }
				// const res = await this.$newHttp.post('api/BrandOwner/SaveBrand',params);
				// this.editTemplate = false;
				// uni.hideLoading();
				// if(res.code === 200){
				// 	uni.showToast({
				// 		title:this.editType === 'edit'?'修改成功':'新增成功',
				// 	});
				// 	this.$refs.paging.reload();
				// }else{
				// 	this.$u.toast(this.editType === 'edit'?'修改失败':'新增失败');
				// }
			},
			// 删除按钮点击
			delTemplateTap(id){
				this.chooseTemplateId = id;
				this.delModel = true;
			},
			// 删除价格品牌
			async delTemplateFn(){
				uni.showLoading({
					title:"loading..."
				})
				let params = {
					shopId:this.storeShop.id,
					id:this.chooseTemplateId,
					isDeleted:false
				}
				const res = await this.$newHttp.delete('api/BrandOwner/DeleteBrand',params);
				console.log(res);
				uni.hideLoading();
				if(res.code === 200){
					uni.showToast({
						title:'删除成功',
					});
					this.$refs.paging.reload();
				}else{
					this.$u.toast('删除失败');
				}
			},
			async queryList(pageNo, pageSize) {
				//组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//模拟请求服务器获取分页数据，请替换成自己的网络请求
				const params = {
					pageIndex: pageNo,
					pageSize: pageSize,
					shopId:this.storeShop.id,
					isDeleted:false
				}
				if(this.searchText!==''){
					params.Name = this.searchText;
				}
				
				uni.showLoading({
					title: '加载中...',
					mask: false
				});
				const res = await this.$newHttp.get('api/BrandOwner/GetBrandsByPage', {params:params} );
				uni.hideLoading({
					title: '加载中...'
				})
				if(res.code === 200){
					this.$refs.paging.complete(res.data.list);
				}else{
					this.$refs.paging.complete(false);
				}
			},
			itemClick(item) {
				console.log('点击了', item.title);
				this.$mRouter.push({ 
					route: '/pages/sale/payorder/createPayOrder?shop='+item.id,
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* 注意:父节点需要固定高度，z-paging的height:100%才会生效 */
	.paryorder-content {
		height: 100%;
		.btn-bottom-group{
			background-color: #fff !important;
			// height: 300rpx;
			width: 100%;
			box-sizing: border-box;
			padding: 50rpx 35rpx;
			.u-hairline-border:after{
				// border: none;
				border-radius: 100rpx;
			}
		}
	}
	.payorder-container{
		background-color: #fff;
		box-sizing: border-box;
		padding: 0 30rpx;
		width:48%;
		margin-bottom: 20rpx;
		border-radius: 10rpx;
		.payorder-header{
			display: flex;
			justify-content: space-between;
		}
		.payorder-section{
			display: flex;
			align-items: center;
			box-sizing: border-box;
			flex-direction: column;
			justify-content: center;
			padding: 30rpx 0 60rpx;
			// margin-bottom: 20rpx;
			.section-image{
				width: 168rpx;
				height: 105rpx;
				display: block;
				margin-bottom: 64rpx;			
				margin-top: 60rpx;
			}
			.section-list{
				// flex: 1;
				width: 100%;
				overflow: hidden;
				// margin-left: 20rpx;
				.section-btn{
					width: 160rpx;
					margin-left: 10rpx;
					height:70rpx;
					border-radius: 35rpx;
					border: 1rpx solid #FFBB1A;
					color: #FFBB1A;
					text-align: center;
					line-height: 70rpx;
				}
				.section-btn-disabled{
					border-color:#999999;
					color: #999999;
				}
				.section-item{
					width: 100%;
					.section-item-name{
						font-size: 32rpx;
						font-weight: 500;
					}
					.section-item-count{
						font-size: 24rpx;
						color: #999;
					}
					
				}
			}
			.section-btn-box{
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				>.btn-text{
					font-size: 20rpx;
					margin-top: 10rpx;
				}
			}
			
		}
		
		
	}


</style>
